<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Content extends CI_Controller {
    private $page;
    public function __construct() {
        parent::__construct();
        $user_data = $this->session->all_userdata();
        if (isset($user_data["logged_in"]) && $user_data["logged_in"] == TRUE) {
            $this->page["header"] = $this->load->view("part/header_login", '', true);
        } else {
            $this->page["header"] = $this->load->view("part/header_no_login", '', true);
        }
    }

    /**
     * Action   :index
     * Purpose  :Return to home page (Home/index)
     * Date     :
     * Author   :pham truong
     */
    public function index() {
        redirect('home/index');
    }

    /**
     * Action   :view
     * Purpose  :View content (article, video, link)
     * Unexpected:
     * 1. Nhập
     * Date     :
     * Author   : Pham Truong
     */
        public function view($id_article) {
        $page = $this->page;
        $data['id'] = $id_article;
        $this->load->model("content_model");
        // get heading from article with ID = $id_article
        //$article = $this->content_model->article($id_article);
//        echo "<pre>";
//        print_r($article);
//        echo "</pre>";
        $data['article'] = $this->content_model->article($id_article);
        //$data['new_content'] =$;
        //get created member
        $data['member'] = $this->content_model->member_create($id_article);
        //get date created article
        $data['created_date'] = $this->content_model->get_date($id_article);
        //get contributor
        $data['contributor'] = $this->content_model->get_contributor($id_article);
//        echo $data['contributor'];
//        echo $new_version;
        $page['content'] = $this->load->view("page/content/view", $data, true);
        $this->load->view("layout", $page);
//        }
    }

    public function history($id) {
        $page = $this->page;
//        if (is_int($id) == FALSE)
//            echo 'id chua dc dang ky khai sinh =.=!';
//        else {
        $this->load->model("content_model");
        $data['bai'] = $this->content_model->get_history($id);
//            echo'<pre>';
//            print_r($data);
//            echo'</pre>';
        $page['content'] = $this->load->view("page/content/history", $data, true);
        $this->load->view("layout", $page);
//        }
    }

    public function view_history($id, $version) {
        $page = $this->page;
        $data['version'] = $version;
        $this->load->model("content_model");
        $data['bai'] = $this->content_model->get_story($id, $version);
        $page['content'] = $this->load->view("page/content/view", $data, true);
        $this->load->view("layout", $page);
    }

    public function create() {
        $page = $this->page;
        $user_data = $this->session->all_userdata();
        if (!isset($user_data["logged_in"]) || $user_data["logged_in"] == FALSE) {
            $page["message"]["error"] = "Bạn không có quyền truy cập trang này.";
            $page["content"] = "";
        } else {
            $submitted = $this->input->post("mySubmit");
            if (!$submitted) {
                $page['content'] = $this->load->view("page/content/create", '', true);
            } else {
                if ($this->input->post("title") == FALSE || $this->input->post("content") == FALSE) {
                    $page['message']['error'] = "Chưa nhập tiêu đề/nội dung!";
                    $page['content'] = $this->load->view("page/content/create", '', true);
                } else {
                    $this->load->model("content_model", "content");
                    $data = array (
                        'title' => $this->input->post('title'),
                        'content' => $this->input->post('content'),
                        'permission' => $this->input->post('permission'),
                        'member_id' => $user_data['member_id']
                    );
                    $id = $this->content->insert_article($data);
                    if ($id)
                        $page['message']['success'] = 'Thêm bài viết mới thành công!. <a href="' . base_url() . 'content/view/'.$id.'">Xem kết quả</a>';
                    else
                        $page['message']['error'] = 'Có lỗi xảy ra trong quá trình tạo bài viết!';
                    $page['content'] = "";
                }
            }
        }
        $this->load->view("layout", $page);
    }
}

/* End of file content.php */
/* Location: ./application/controllers/content.php */